package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String REDIS_KEYS = "dish_*";
    private static final String REDIS_KEY = "dish_";


    /**
     * 新增菜品接口
     */
    @PostMapping
    @ApiOperation(value = "新增菜品接口")
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);

        //清除缓存
        String key = REDIS_KEY+dishDTO.getCategoryId();

        clearCache(key);

        return Result.success();
    }
    /**
     * 菜品分页查询接口
     */
    @GetMapping("/page")
    @ApiOperation(value = "菜品分页查询接口")
    //Query传参
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品接口：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除菜品接口
     */
    @DeleteMapping
    @ApiOperation(value = "删除菜品接口")
    public Result deleteById(@RequestParam List<Long> ids) {
        log.info("删除菜品接口：{}", ids);
        dishService.deleteBatch(ids);
        //清除所有缓存
        clearCache(REDIS_KEYS);
        return Result.success();
    }
    /**
     * 修改菜品接口
     */
    @PutMapping
    @ApiOperation(value = "修改菜品接口")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品接口：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        //清除缓存
        clearCache(REDIS_KEYS);

        return Result.success();
    }

    /**
     * 根据id查询菜品接口
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜品接口")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品接口：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 启用/禁用菜品接口
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用/禁用菜品接口")
    public Result update(@PathVariable Integer status,Long id){
        log.info("修改分类状态,id:{},status:{}",id,status);
        dishService.startOrStop(status,id);

        //清除所有缓存
        clearCache(REDIS_KEYS);

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


    /**
     *  清理缓存
     * @param pattern
     */
    private void clearCache(String pattern){
        //清除所有缓存
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
